package com.bjsdzk.common.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 胜顶智控平台管理
 * Calendar时间工具类
 *
 * @author chuntao Liu
 * @time 2019/6/28.
 */
public class CalendarUtils {
    public static Calendar build() {
        return Calendar.getInstance();
    }

    public static Calendar build(Date date) {
        Calendar calendar = build();
        calendar.setTime(date);
        return calendar;
    }

    public static Calendar build(long time) {
        Calendar calendar = build();
        calendar.setTime(new Date(time));
        return calendar;
    }

    public static Calendar build(int year, int month, int day, int hour, int minutes, int seconds, int miSeconds) {
        Calendar ca = Calendar.getInstance();
        ca.set(year, month - 1, day, hour, minutes, seconds);
        ca.set(Calendar.MILLISECOND, miSeconds);

        return ca;
    }

    public static Date build(Date date, int year, int month, int day, int hour, int minutes, int seconds, int miSeconds) {
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        if (year != -1)
            ca.set(Calendar.YEAR, year);
        if (month != -1)
            ca.set(Calendar.MONTH, month);
        if (day != -1)
            ca.set(Calendar.DAY_OF_MONTH, day);
        if (hour != -1)
            ca.set(Calendar.HOUR_OF_DAY, hour);
        if (minutes != -1)
            ca.set(Calendar.MINUTE, minutes);
        if (seconds != -1)
            ca.set(Calendar.SECOND, seconds);
        if (miSeconds != -1)
            ca.set(Calendar.MILLISECOND, miSeconds);

        return ca.getTime();
    }


    public static Date build(Long date, int year, int month, int day, int hour, int minutes, int seconds, int miSeconds) {
        if (date == null) {
            return null;
        }
        Calendar ca = Calendar.getInstance();
        ca.setTimeInMillis(date);
        if (year != -1)
            ca.set(Calendar.YEAR, year);
        if (month != -1)
            ca.set(Calendar.MONTH, month);
        if (day != -1)
            ca.set(Calendar.DAY_OF_MONTH, day);
        if (hour != -1)
            ca.set(Calendar.HOUR_OF_DAY, hour);
        if (minutes != -1)
            ca.set(Calendar.MINUTE, minutes);
        if (seconds != -1)
            ca.set(Calendar.SECOND, seconds);
        if (miSeconds != -1)
            ca.set(Calendar.MILLISECOND, miSeconds);

        return ca.getTime();
    }

    public static Calendar build(int year, int month, int day) {
        Calendar ca = Calendar.getInstance();
        ca.set(year, month - 1, day, 0, 0, 0);
        ca.set(Calendar.MILLISECOND, 0);

        return ca;
    }

    public static Calendar build(int year, int month) {
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.YEAR, year);
        ca.set(Calendar.MONTH, month - 1);

        return ca;
    }

    public static Date addHours(Date date, int step) {
        Calendar calendar = build(date);
        calendar.add(Calendar.HOUR_OF_DAY, step);
        return calendar.getTime();
    }

    public static Date addMinutes(Date date, int step) {
        Calendar calendar = build(date);
        calendar.add(Calendar.MINUTE, step);
        return calendar.getTime();
    }

    public static void addHours(Calendar ca, int step) {
        ca.add(Calendar.HOUR_OF_DAY, step);
    }

    public static Date addDays(Date date, int step) {
        Calendar calendar = build(date);
        calendar.add(Calendar.DAY_OF_MONTH, step);
        return calendar.getTime();
    }

    public static void addDays(Calendar ca, int step) {
        ca.add(Calendar.DAY_OF_MONTH, step);
    }

    public static Date addMonths(Date date, int step) {
        Calendar calendar = build(date);
        calendar.add(Calendar.MONTH, step);
        return calendar.getTime();
    }

    public static void addMonths(Calendar ca, int step) {
        ca.add(Calendar.MONTH, step);
    }

    public static Date addYears(Date date, int step) {
        Calendar calendar = build(date);
        calendar.add(Calendar.YEAR, step);
        return calendar.getTime();
    }

    public static void addYears(Calendar ca, int step) {
        ca.add(Calendar.YEAR, step);
    }

    public static int getYear() {
        return build().get(Calendar.YEAR);
    }

    public static int getMonth() {
        return build().get(Calendar.MONTH);
    }

    public static int getDay() {
        return build().get(Calendar.DAY_OF_MONTH);
    }

    public static int getHour() {
        return build().get(Calendar.HOUR_OF_DAY);
    }
}
